package com.jxb.four;

import java.util.LinkedList;
import java.util.List;

import com.jxb.tree.TreeNode;

/**
 * 类注释
 *
 * @author jiaobo
 * @date Created in 2025/1/1 16:18
 **/
public class BinTreeZigzagOrderTraversal_103 {

    public static void main(String[] args) {
        System.out.println(11%2);
    }

    public List<List<Integer>> levelOrder(TreeNode root) {
        //输出结果
        List<List<Integer>> res = new LinkedList<>();
        if (root == null) {
            return res;
        }
        levelPreVisit(res,root,0);
        return res;
    }

    private void levelPreVisit(List<List<Integer>> res, TreeNode root, int height) {
        if (root == null) {
            return;
        }
        if (height >= res.size()) {
            res.add(new LinkedList<>());
        }
        //判断层高是奇数还是偶数
        //偶数，尾插法
        if (height%2 == 0) {
            res.get(height).add(root.val);
        }else {
            //奇数，头插法
            res.get(height).add(0, root.val);
        }

        levelPreVisit(res,root.left,height+1);
        levelPreVisit(res,root.right,height+1);
    }

}
